Imports DotNetPM.Business.Questionnaire

Partial Class Modules_Questionnaires_Controls_ansMultipleChoicesMultipleAnswers
    Inherits System.Web.UI.UserControl
    Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC

    Private _config, _answerType, _questionId, _sortId As String
    Private _controlType As Boolean
    Private rlChoices As RadioButtonList
    Private ddlChoices As DropDownList
    Private _controlLoaded As Boolean = False

    Public Property Answer() As String Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC.Answer
        Get
            'LoadAnswerControl()
            Dim _ans As String = String.Empty
            Dim _first As Boolean = True
            For Each choice As ListItem In cblChoices.Items
                If choice.Selected Then
                    If _first Then
                        _ans &= choice.Value
                        _first = False
                    Else
                        _ans &= "," & choice.Value
                    End If
                End If
            Next
            Return _ans
        End Get
        Set(ByVal value As String)
            If String.IsNullOrEmpty(value) Then
                Exit Property
            End If
            Dim vals As String() = value.Split(",")
            For Each choice As ListItem In cblChoices.Items
                If CheckValue(choice, vals) Then
                    choice.Selected = True
                End If
            Next
        End Set
    End Property

    Private Function CheckValue(ByVal li As ListItem, ByVal vals As String()) As Boolean
        For Each val As String In vals
            If li.Value = val Then
                Return True
            End If
        Next
        Return False
    End Function

    Public Property Config() As String Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC.Config
        Get
            Return _config
        End Get
        Set(ByVal value As String)
            If Not String.IsNullOrEmpty(value) Then
                _config = value
            End If
        End Set
    End Property

    Private Sub LoadAnswerControl()
        If _controlLoaded Then
            Exit Sub
        End If
        Dim val As String() = Split(Me.Config, "||")
        _sortId = val(0)
        _questionId = val(1)
        _answerType = val(2)
        Dim _minAnswer As Integer = 0
        If Not String.IsNullOrEmpty(val(3)) Then
            _minAnswer = CInt(val(3))
        End If
        Dim _maxAnswer As Integer = 0
        If Not String.IsNullOrEmpty(val(4)) Then
            _maxAnswer = CInt(val(4))
        End If

        With cblChoices
            .RepeatColumns = val(5)
            .RepeatDirection = val(6)
            .RepeatLayout = val(7)
            .DataSource = QuestionODS.GetChoices(_questionId)
            .DataTextField = "Choice"
            .DataValueField = "SortID"
            .DataBind()
        End With
        With cblVal
            If _maxAnswer = 0 AndAlso _minAnswer = 0 Then
                .Visible = False
            Else
                .MaximumSelections = _maxAnswer
                .MinimumSelections = _minAnswer
                .Text = "*"
                .ErrorMessage = "Invalid selection! ("
                ' Min:1, Max:2
                If .MaximumSelections > 0 Then
                    If .MinimumSelections > 0 Then
                        .ErrorMessage &= "minimum of " & _minAnswer & ", and maximum of " & _maxAnswer & ")"
                    Else
                        .ErrorMessage &= "maximum of " & _maxAnswer & ")"
                    End If
                Else
                    If .MinimumSelections > 0 Then
                        .ErrorMessage &= "minimum of " & _minAnswer & ")"
                    End If
                End If
            End If
        End With
        _controlLoaded = True
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadAnswerControl()
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        LoadAnswerControl()
    End Sub
End Class
